package com.atguigu.yygh.hosp.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.yygh.common.exception.YyghException;
import com.atguigu.yygh.hosp.repository.DepartmentRepository;
import com.atguigu.yygh.hosp.service.DepartmentService;
import com.atguigu.yygh.model.hosp.Department;
import com.atguigu.yygh.vo.hosp.DepartmentVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author mgx
 * @version 1.0
 * @description: TODO
 * @date 2022/8/27 12:00
 */
@Service
public class DepartmentServiceImpl implements DepartmentService {

    @Autowired
    private DepartmentRepository departmentRepository;

    @Override
    public void saveDepartment(Map<String, Object> paramMap) {
        // 转化map为pojo数据 --> 新增或者修改的数据
        Department department = JSONObject.parseObject(JSONObject.toJSONString(paramMap), Department.class);

        // 根据hoscode + depcode 联合查询判断该医院下的这个科室是否存在
        //      存在就修改，不存在就添加
        String hoscode = department.getHoscode();
        String depcode = department.getDepcode();
        Department platformDepartment = departmentRepository.findByHoscodeAndDepcode(hoscode, depcode);
        if(platformDepartment == null) {
            // 没有查到，添加操作
            department.setCreateTime(new Date());
            department.setUpdateTime(new Date());
            department.setIsDeleted(0);
            departmentRepository.save(department);
        }else {
            // 查询到了修改操作
            department.setId(platformDepartment.getId());
            department.setCreateTime(platformDepartment.getCreateTime());
            department.setUpdateTime(new Date());
            department.setIsDeleted(platformDepartment.getIsDeleted());
            departmentRepository.save(department);
        }
    }

    @Override
    public Page<Department> getDepartmentPage(Map<String, Object> paramMap) {
        // 获取有用参数
        String hoscode = (String)paramMap.get("hoscode");
        int pageNum = Integer.parseInt((String) paramMap.get("page"));
        int size = Integer.parseInt((String) paramMap.get("limit"));
        // 条件查询
        Department department = new Department();
        department.setHoscode(hoscode);
        Example<Department> example = Example.of(department);
        // 注意：0表示第一页
        Pageable pageInfo = PageRequest.of(pageNum - 1, size);

        Page<Department> all = departmentRepository.findAll(example, pageInfo);
        return all;
    }

    @Override
    public void remove(Map<String, Object> paramMap) {
        // 获取参数信息
        String hoscode = (String)paramMap.get("hoscode");
        String depcode = (String)paramMap.get("depcode");
        // 先根据参数查询科室信息
        Department department = departmentRepository.findByHoscodeAndDepcode(hoscode, depcode);
        if (department != null) {
            departmentRepository.deleteById(department.getId());
        }else {
            throw new YyghException(201, "该医院科室信息不存在");
        }
    }

    @Override
    public List<DepartmentVo> getDepartmentList(String hoscode) {
        // 根据医院编号查询所有科室信息
        Department department = new Department();
        department.setHoscode(hoscode);
        Example<Department> example = Example.of(department);
        List<Department> all = departmentRepository.findAll(example);

        // 此时需要把所有父子科室进行分离 --- 根据每个科室的bigcode进行分离
        // 使用Stream
        Map<String, List<Department>> deptMap = all.stream().collect(Collectors.groupingBy(Department::getBigcode));
        // 此时的map的key对应的是bigcode【大科室编号】
        // value对应的是bigcode底下的Children列表【小科室编号】

        // 需要封装到Vo对象中
        // 大科室列表
        List<DepartmentVo> bigDepartmentList = new ArrayList<>();
        Set<Map.Entry<String, List<Department>>> entries = deptMap.entrySet();
        for (Map.Entry<String, List<Department>> entry : entries) {
            // 创建一个大科室vo对象
            DepartmentVo bigDepartment = new DepartmentVo();
            String bigcode = entry.getKey();
            // value对应的就是它的子科室
            List<Department> value = entry.getValue();

            // 获取它的小科室列表
            List<DepartmentVo> childDepartmentList = new ArrayList<>();
            for (Department dept : value) {
                // 创建一个小科室vo对象
                DepartmentVo smallDepartment = new DepartmentVo();
                smallDepartment.setDepname(dept.getDepname());
                smallDepartment.setDepcode(dept.getDepcode());
                // 小科室是没有小科室的
                childDepartmentList.add(smallDepartment);
            }

            // 封装大科室数据
            bigDepartment.setDepcode(bigcode);
            bigDepartment.setDepname(value.get(0).getBigname());
            bigDepartment.setChildren(childDepartmentList);
            // 加入大科室列表
            bigDepartmentList.add(bigDepartment);

        }

        return bigDepartmentList;
    }

    @Override
    public String getDepName(String hoscode, String depcode) {
        Department department = departmentRepository.findByHoscodeAndDepcode(hoscode, depcode);
        return department.getDepname();
    }

    @Override
    public Department getDepartment(String hoscode, String depcode) {
        return departmentRepository.findByHoscodeAndDepcode(hoscode,depcode);
    }

}
